package com.virtualparadigm.hierarchy;

import java.util.Iterator;
import java.util.StringTokenizer;

public class LineageListParser
{
	private static String DEFAULT_DELIMITER = "/";
	
	public static LineageList<String> parse(String str, String delimiter, boolean parentFirst)
	{
		LineageList<String> lList = null;
		if(str != null)
		{
			lList = new LineageList<String>();
			if(delimiter == null)
			{
				lList.pushParent(str);
			}
			else
			{
				StringTokenizer st = new StringTokenizer(str, delimiter);
				while(st.hasMoreElements())
				{
					if(parentFirst)
					{
						lList.queueChild(st.nextToken());
					}
					else
					{
						lList.pushParent(st.nextToken());
					}
				}
			}
		}
		return lList;
	}
	
	public static String toString(LineageList<? extends Object> hList, String delimiter, boolean parentsFirst)
	{
		StringBuffer sb = new StringBuffer();
		if(hList != null)
		{
			if(delimiter == null)
			{
				delimiter = LineageListParser.DEFAULT_DELIMITER;
			}
			
			Iterator<? extends Object> it=null;
			if(parentsFirst)
			{
				it=hList.getParentsFirstIterator();
			}
			else
			{
				it=hList.getChildrenFirstIterator();
			}
			if(it != null)
			{
				if(it.hasNext())
				{
					sb.append(it.next().toString());
				}
				while(it.hasNext())
				{
					sb.append(delimiter + it.next().toString());
				}
			}
		}
		return sb.toString();
	}
	
	
	public static void main(String[] args)
	{
		String str = "foo.bar.yo";
		LineageList<String> hList = LineageListParser.parse(str, ".", true);
		System.out.println("str:" + str + " result: " + LineageListParser.toString(hList, "/", true));
		
		str = "foo.bar.";
		hList = LineageListParser.parse(str, ".", true);
		System.out.println("str:" + str + " result: " + LineageListParser.toString(hList, "/", true));

		str = "foo.bar";
		hList = LineageListParser.parse(str, ".", true);
		System.out.println("str:" + str + " result: " + LineageListParser.toString(hList, "/", true));

		str = "";
		hList = LineageListParser.parse(str, ".", true);
		System.out.println("str:" + str + " result: " + LineageListParser.toString(hList, "/", true));

		str = null;
		hList = LineageListParser.parse(str, ".", true);
		System.out.println("str:" + str + " result: " + LineageListParser.toString(hList, "/", true));


		
		str = "foo.bar.yo";
		hList = LineageListParser.parse(str, ".", false);
		System.out.println("str:" + str + " result: " + LineageListParser.toString(hList, "/", true));
		
		str = "foo.bar.";
		hList = LineageListParser.parse(str, ".", false);
		System.out.println("str:" + str + " result: " + LineageListParser.toString(hList, "/", true));

		str = "foo.bar";
		hList = LineageListParser.parse(str, ".", false);
		System.out.println("str:" + str + " result: " + LineageListParser.toString(hList, "/", true));

		str = "";
		hList = LineageListParser.parse(str, ".", false);
		System.out.println("str:" + str + " result: " + LineageListParser.toString(hList, "/", true));

		str = null;
		hList = LineageListParser.parse(str, ".", false);
		System.out.println("str:" + str + " result: " + LineageListParser.toString(hList, "/", true));

		
		
		
		str = "foo.bar.yo";
		hList = LineageListParser.parse(str, ".", true);
		System.out.println("str:" + str + " result: " + LineageListParser.toString(hList, "/", false));
		
		str = "foo.bar.";
		hList = LineageListParser.parse(str, ".", true);
		System.out.println("str:" + str + " result: " + LineageListParser.toString(hList, "/", false));

		str = "foo.bar";
		hList = LineageListParser.parse(str, ".", true);
		System.out.println("str:" + str + " result: " + LineageListParser.toString(hList, "/", false));

		str = "";
		hList = LineageListParser.parse(str, ".", true);
		System.out.println("str:" + str + " result: " + LineageListParser.toString(hList, "/", false));

		str = null;
		hList = LineageListParser.parse(str, ".", true);
		System.out.println("str:" + str + " result: " + LineageListParser.toString(hList, "/", false));
	}
	
}